package com.smart.lab.view.common.intercept;

import android.util.Log;

import com.smart.lab.utils.UserInfoUtils;

import java.io.IOException;
import java.util.HashSet;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * Created by liuyan on 2019/9/27.
 */

public class ReceivedCookiesInterceptor implements Interceptor {

    private static final String TAG=ReceivedCookiesInterceptor.class.getSimpleName();
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());

        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            HashSet<String> cookies = new HashSet<>();

            for (String header : originalResponse.headers("Set-Cookie")) {
                cookies.add(header);
            }

            UserInfoUtils.setCookies(cookies);
        }else {
            Log.d(TAG,"request:"+originalResponse.request().url().toString());
        }

        return originalResponse;
    }
}